@echo off
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rem  Package:		OurGrid 4.0
rem  Description:	This batch script is used to start a Virtual Box 
rem                 virtual machine. It makes use of the VBOX_OPTS file
rem 				to determine the shared folder�s name.
rem  Copyright (c)  2007 Universidade Federal de Campina Grande 
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if %1.==/?. goto usage
if %3.==. goto usage
goto findself

:findself
set SELF=%~d0%~p0
set SHARED=ourgridshared
goto main

:main
set VMANAGE=%1
set IMAGE=%2
set FOLDER=%3


rem %VMANAGE% snapshot %IMAGE% discardcurrent --state

%VMANAGE% sharedfolder remove %IMAGE% -name %SHARED%

%VMANAGE% sharedfolder add %IMAGE% -name %SHARED% -hostpath %FOLDER%
if not %ERRORLEVEL% == 0 (
	echo Unable to setup shared folder 1>&2
rem	exit 1
)

for %%i in (1 2 3 4 5 6 7 8 9 10) do (
	%VMANAGE% startvm %IMAGE% 
	if %ERRORLEVEL% == 0 goto waitr
	call "%SELF%\sleep.bat" 2
)
rem exit 1
goto eof

:waitr
%VMANAGE% showvminfo %IMAGE% | find "running"
if not %ERRORLEVEL% == 0 (
	call "%SELF%\sleep.bat" 2
	goto waitr
)
goto eof


:usage
echo usage: 1>&2
echo [CALL] %0 ^<VMANAGE COMMAND^> ^<VBOX IMAGE NAME^> ^<VBOX SHARED FOLDER^> 1>&2

:eof
